package service
import (
	"go_code/projectExec/customer/model"
)
type CustomerService struct{
	customers []model.Customer
	customerNum int
}
//结构体--工厂方法
func CustomerServiceFactory()*CustomerService{
	customerService :=&CustomerService{}
	customerService.customerNum = 1
	customer := model.CustomerFactory(1,"fmx","男",20,"1213123","10086@qq.com")
	customerService.customers = append(customerService.customers,customer)
	return customerService
}
//返回客户信息的业务方法
func (this *CustomerService)List() []model.Customer{
	return this.customers
} 
//添加用户的业务方法
func (this *CustomerService)Add(customer model.Customer) bool{
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers,customer)
	return true
}
//删除用户的业务方法
func (this *CustomerService)Delete(id int)bool{
	index := this.FindById(id)
	if index == -1{
		return false
	}else{
		this.customers = append(this.customers[:index],this.customers[index+1:]...)
		return true
	}
}
//按id查找用户的业务方法
func (this *CustomerService)FindById(id int) int{
	index := -1
	for i:= 0;i < len(this.customers);i++{
		if id == this.customers[i].Id{
			index = i
		}
	}
	return index
}
//按id更改用户信息的业务方法
func (this *CustomerService)Update(c model.Customer,id int)bool{
	this.customers[id] = c
	return true
}
func (this *CustomerService)FindCustomerById(id int)(int,model.Customer){
	index := this.FindById(id)
	if index == -1{
		return index,model.Customer{}
	}else{
		return index,this.customers[index]
	}
}